#
# This file is a part of the open-eBackup project.
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the MPL was not distributed with this file, You can obtain one at
# http://mozilla.org/MPL/2.0/.
#
# Copyright (c) [2024] Huawei Technologies Co.,Ltd.
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
#

import sys
from clickhouse import log
from clickhouse.common.clickhouse_constants import SysData
from clickhouse.services.clickhouse_backup_service import ClickHouseBackupService
from clickhouse.services.clickhouse_resource_service import ClickHouseResourceService
from clickhouse.services.clickhouse_restore_service import ClickHouseRestoreService
from common.cleaner import clear
from common.common import exter_attack
from common.util.check_utils import is_valid_id


class ClickHouseApi:
    @staticmethod
    @exter_attack
    def check_application(pid, *args):
        ClickHouseResourceService.check_application(pid)

    @staticmethod
    @exter_attack
    def query_cluster(pid, *args):
        ClickHouseResourceService.query_cluster(pid)

    @staticmethod
    @exter_attack
    def list_application_v2(pid, *args):
        ClickHouseResourceService.list_application_v2(pid)

    @staticmethod
    @exter_attack
    def check_backup_job_type(pid, job_id, *args):
        ClickHouseBackupService.check_backup_job_type(pid, job_id)

    @staticmethod
    @exter_attack
    def query_job_permission(pid, job_id, *args):
        ClickHouseBackupService.query_job_permission(pid, job_id)

    @staticmethod
    @exter_attack
    def allow_backup_in_local_node(pid, job_id, sub_job_id):
        ClickHouseBackupService.allow_backup_in_local_node(pid, job_id, sub_job_id)

    @staticmethod
    @exter_attack
    def allow_backup_sub_job_in_local_node(pid, job_id, sub_job_id):
        ClickHouseBackupService.allow_backup_sub_job_in_local_node(pid, job_id, sub_job_id)

    @staticmethod
    @exter_attack
    def backup_prerequisite(pid, job_id, *args):
        ClickHouseBackupService.backup_prerequisite(pid, job_id)

    @staticmethod
    @exter_attack
    def backup_prerequisite_progress(pid, job_id, sub_job_id):
        ClickHouseBackupService.backup_prerequisite_progress(pid, job_id, sub_job_id)

    @staticmethod
    @exter_attack
    def backup_gen_sub_job(pid, job_id, *args):
        ClickHouseBackupService.backup_gen_sub_job(pid, job_id)

    @staticmethod
    @exter_attack
    def backup(pid, job_id, sub_job_id):
        ClickHouseBackupService.backup(pid, job_id, sub_job_id)

    @staticmethod
    @exter_attack
    def backup_post_job(pid, job_id, *args):
        ClickHouseBackupService.backup_post_job(pid, job_id)

    @staticmethod
    @exter_attack
    def backup_post_job_progress(pid, job_id, sub_job_id):
        ClickHouseBackupService.backup_post_job_progress(pid, job_id, sub_job_id)

    @staticmethod
    @exter_attack
    def abort_job(pid, job_id, sub_job_id):
        ClickHouseBackupService.abort_job(pid, job_id, sub_job_id)

    @staticmethod
    @exter_attack
    def pause_job(pid, job_id, *args):
        ClickHouseBackupService.pause_job(pid, job_id)

    @staticmethod
    @exter_attack
    def query_backup_copy(pid, job_id, *args):
        ClickHouseBackupService.query_backup_copy(pid, job_id)

    @staticmethod
    @exter_attack
    def allow_restore_in_local_node(pid, job_id, sub_job_id):
        ClickHouseRestoreService.allow_restore_in_local_node(pid, job_id, sub_job_id)

    @staticmethod
    @exter_attack
    def allow_restore_sub_job_in_local_node(pid, job_id, sub_job_id):
        ClickHouseRestoreService.allow_restore_sub_job_in_local_node(pid, job_id, sub_job_id)

    @staticmethod
    @exter_attack
    def restore_prerequisite(pid, job_id, sub_job_id):
        ClickHouseRestoreService.restore_prerequisite(pid, job_id, sub_job_id)

    @staticmethod
    @exter_attack
    def restore_prerequisite_progress(pid, job_id, sub_job_id):
        ClickHouseRestoreService.restore_prerequisite_progress(pid, job_id, sub_job_id)

    @staticmethod
    @exter_attack
    def restore(pid, job_id, sub_job_id):
        ClickHouseRestoreService.restore(pid, job_id, sub_job_id)

    @staticmethod
    @exter_attack
    def restore_progress(pid, job_id, sub_job_id):
        ClickHouseRestoreService.restore_progress(pid, job_id, sub_job_id)

    @staticmethod
    @exter_attack
    def restore_post_job(pid, job_id, sub_job_id):
        ClickHouseRestoreService.restore_post_job(pid, job_id, sub_job_id)

    @staticmethod
    @exter_attack
    def restore_post_job_progress(pid, job_id, sub_job_id):
        ClickHouseRestoreService.restore_post_job_progress(pid, job_id, sub_job_id)

    @staticmethod
    @exter_attack
    def restore_gen_sub_job(pid, job_id, sub_job_id):
        ClickHouseRestoreService.restore_gen_sub_job(pid, job_id, sub_job_id)


func_dict = {
    'CheckApplication': ClickHouseApi.check_application,
    'QueryCluster': ClickHouseApi.query_cluster,
    'ListApplicationResourceV2': ClickHouseApi.list_application_v2,
    'AllowBackupInLocalNode': ClickHouseApi.allow_backup_in_local_node,
    'AllowBackupSubJobInLocalNode': ClickHouseApi.allow_backup_sub_job_in_local_node,
    'CheckBackupJobType': ClickHouseApi.check_backup_job_type,
    'QueryJobPermission': ClickHouseApi.query_job_permission,
    'BackupPrerequisite': ClickHouseApi.backup_prerequisite,
    'BackupPrerequisiteProgress': ClickHouseApi.backup_prerequisite_progress,
    'BackupGenSubJob': ClickHouseApi.backup_gen_sub_job,
    'Backup': ClickHouseApi.backup,
    'BackupPostJob': ClickHouseApi.backup_post_job,
    'BackupPostJobProgress': ClickHouseApi.backup_post_job_progress,
    'QueryBackupCopy': ClickHouseApi.query_backup_copy,
    'AbortJob': ClickHouseApi.abort_job,
    'PauseJob': ClickHouseApi.pause_job,
    'AllowRestoreInLocalNode': ClickHouseApi.allow_restore_in_local_node,
    'AllowRestoreSubJobInLocalNode': ClickHouseApi.allow_restore_sub_job_in_local_node,
    'RestorePrerequisite': ClickHouseApi.restore_prerequisite,
    'RestorePrerequisiteProgress': ClickHouseApi.restore_prerequisite_progress,
    'RestoreGenSubJob': ClickHouseApi.restore_gen_sub_job,
    'Restore': ClickHouseApi.restore,
    'RestoreProgress': ClickHouseApi.restore_progress,
    'RestorePostJob': ClickHouseApi.restore_post_job,
    'RestorePostJobProgress': ClickHouseApi.restore_post_job_progress
}

if __name__ == '__main__':
    """
     功能描述：主任务执行
     参数：
     @xxx.py：argv[0] clickhouse_api.py python文件
     @fun_type：argv[1] 方法类型
     @pid：argv[2] 请求ID
     @job_id：argv[3] 主任务任务ID
     @sub_job_id: argv[4] 子任务ID
     例：python3 clickhouse_api.py fun_type pid job_id sub_job_id
    """
    for line in sys.stdin:
        SysData.SYS_STDIN = line
        break
    g_fun_type = sys.argv[1]
    g_pid = sys.argv[2]
    if not is_valid_id(g_pid):
        log.warn(f'g_pid is invalid')
        clear(sys.stdin)
        sys.exit(1)
    g_job_id = ''
    g_sub_job_id = ''
    if len(sys.argv) >= 4:
        g_job_id = sys.argv[3]
        if not is_valid_id(g_job_id):
            log.warn(f'g_job_id is invalid')
            clear(sys.stdin)
            sys.exit(1)
    if len(sys.argv) >= 5:
        g_sub_job_id = sys.argv[4]
        if not is_valid_id(g_sub_job_id):
            log.warn(f'g_sub_job_id is invalid')
            clear(sys.stdin)
            sys.exit(1)
    try:
        log.info(f'######## function: {g_fun_type} start, pid: {g_pid}, job_id: {g_job_id}, sub_job_id: {g_sub_job_id}')
        func_dict[g_fun_type](g_pid, g_job_id, g_sub_job_id)
        log.info(f'######## function: {g_fun_type} end, pid: {g_pid}')
    except KeyError:
        log.error(f'######## function: {g_fun_type} not exist')
    finally:
        clear(SysData.SYS_STDIN)
